package com.example.forestmanagement.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.forestmanagement.model.LocationPoint;

import java.util.ArrayList;
import java.util.List;

// LocationDBHelper.java
public class LocationDBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "locations.db";
    private static final int DATABASE_VERSION = 2;
    public static final String TABLE_NAME = "locations";

    public static final String COLUMN_ID = "id";
    public static final String COLUMN_USER_ID = "userId";
    public static final String COLUMN_LATITUDE = "latitude";
    public static final String COLUMN_LONGITUDE = "longitude";
    public static final String COLUMN_CREATE_TIME = "createTime";
    public static final String COLUMN_IS_SYNCED = "isSynced";

    public LocationDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_USER_ID + " INTEGER, " +
                COLUMN_LATITUDE + " REAL, " +
                COLUMN_LONGITUDE + " REAL, " +
                COLUMN_CREATE_TIME + " INTEGER, " +
                COLUMN_IS_SYNCED + " INTEGER DEFAULT 0)";
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
            db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_CREATE_TIME + " INTEGER");
            db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_IS_SYNCED + " INTEGER DEFAULT 0");
        }
    }

    public void addLocation(LocationPoint point) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_USER_ID, point.getUserId());
        values.put(COLUMN_LATITUDE, point.getLatitude());
        values.put(COLUMN_LONGITUDE, point.getLongitude());
        values.put(COLUMN_CREATE_TIME, System.currentTimeMillis());
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public List<LocationPoint> getUnsyncedLocations() {
        List<LocationPoint> points = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME,
                new String[]{COLUMN_ID, COLUMN_USER_ID, COLUMN_LATITUDE, COLUMN_LONGITUDE, COLUMN_CREATE_TIME},
                COLUMN_IS_SYNCED + " = ?", new String[]{"0"}, null, null, null);

        while (cursor.moveToNext()) {
            LocationPoint point = new LocationPoint(
                    cursor.getInt(0),
                    cursor.getInt(1),
                    cursor.getDouble(2),
                    cursor.getDouble(3),
                    cursor.getLong(4)
            );
            points.add(point);
        }
        cursor.close();
        db.close();
        return points;
    }

    public void markLocationAsSynced(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_IS_SYNCED, 1);
        db.update(TABLE_NAME, values, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
        db.close();
    }
}
